package com.zero.controller;

import com.zero.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/weather")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    /**
     * 通过缓存的方式获取天气信息
     * @param city 城市
     * @return
     */
    @RequestMapping("/query")
    public String getWeather(String city) {
        return weatherService.getWeatherByCity(city);
    }

    /**
     * 不使用缓存的方式获取天气信息
     * @param city 城市
     * @return
     */
    @RequestMapping("/queryWithoutCache")
    public String getWeatherWithoutCache(String city) {
        return weatherService.getWeatherWithoutCacheByCity(city);
    }

    /**
     * 更新缓存中的天气
     * @param city 城市
     * @param weather 天气
     * @return 更新结果
     */
    @RequestMapping("/update")
    public String updateWeather(String city, String weather){
        String result = weatherService.manuallyUpdateWeatherCacheByCity(city, weather);
        return String.format("update %s's weather to %s", city, result);
    }

    /**
     * 删除缓存中的天气
     * @param city 城市
     * @return 删除结果
     */
    @RequestMapping("/delete")
    public String deleteWeather(String city){
        return weatherService.manuallyDeleteWeatherCacheByCity(city);
    }
}
